package cabbage.database.operations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cabbage.database.idao.IAccountDAO;
import cabbage.database.utilities.DataBaseConnection;

public class AccountDAO implements IAccountDAO {

	private DataBaseConnection db;

	public int insertNewAccount(String username, String firstname, String lastname, String emailaddress, String password) throws SQLException {

		db = new DataBaseConnection();
		Connection con = db.getConnection();

		PreparedStatement preparedStatement = 
				con.prepareStatement("insert into account (username,firstname,lastname,emailaddress,password) values (?,?,?,?,?)",
						Statement.RETURN_GENERATED_KEYS);
		preparedStatement.setString(1, username);
		preparedStatement.setString(2, firstname);
		preparedStatement.setString(3, lastname);
		preparedStatement.setString(4, emailaddress);
		preparedStatement.setString(5, password);
		preparedStatement.execute();

		ResultSet rskey = preparedStatement.getGeneratedKeys();

		if (rskey.next()) {
			int newAccountID = rskey.getInt(1);
			con.close();
			
			return newAccountID;
		}

		con.close();

		return 0;
	}

	

	@Override
	public int createAccount(String username, String firstName,
			String lastName, String emailAddress, String password) throws SQLException {
		// TODO Auto-generated method stub
		return insertNewAccount(username, firstName, lastName, emailAddress, password);
	}

	@Override
	public boolean checkEmailExists(String emailAddess) throws SQLException {
		db = new DataBaseConnection();
		Connection conn = db.getConnection();

		PreparedStatement ps = conn.prepareStatement("SELECT emailaddress from account where emailaddress=?");
		ps.setString(1, emailAddess);

		ResultSet rs = ps.executeQuery();

		if (rs.next()) {
			conn.close();

			return true;
		}
		conn.close();

		return false;
	}

	@Override
	public boolean verifyLogin(String username, String password) throws SQLException {
		db = new DataBaseConnection();
		Connection conn = db.getConnection();

		PreparedStatement ps = conn.prepareStatement("SELECT username,password from account where username=? and password=?");
		ps.setString(1, username);
		ps.setString(2, password);

		ResultSet rs = ps.executeQuery();

		if (rs.next()) {
			conn.close();

			return true;
		}

		conn.close();

		return false;
	}
	
//	public int createPlayer(int accountid) throws SQLException {
//
//		db = new DataBaseConnection();
//		Connection con = db.getConnection();
//
//		PreparedStatement preparedStatement = 
//				con.prepareStatement("insert into player (account_id) values (?)",
//						Statement.RETURN_GENERATED_KEYS);
//		preparedStatement.setInt(1, accountid);
//		preparedStatement.execute();
//		
//		ResultSet rskey = preparedStatement.getGeneratedKeys();
//
//		if (rskey.next()) {
//			int newPlayerID = rskey.getInt(1);
//			con.close();
//			
//			return newPlayerID;
//		}
//		
//		con.close();
//		
//		return 0;
//	}
	
	

}
